<?php

/**
 * 同步缓存
 *
 * @author    Y.N
 * @copyright    www.360hqb.com
 * @access       public
 */
class Sync_Cache
{
    // 缓存路径
    private $_cache_path;

    // 设置数据缓存的时间,单位:小时;0表示不缓存
    private $_cache_time = 0;

	/**
	 * 构造方法
	 *
	 * @param	none
	 * @access	public
	 * @return	void
	 * @update	2011/3/24
	*/
	public function __construct()
	{
		// 缓存过期时间为三天
	    $this->set_cache_time(240);

		// 设置缓存目录
		$this->set_cache_path(TMPPATH.'cache/cli');


	} // end func

	/**
	 * 设置缓存时间
	 *
	 * @param	int  $time (单位：小时)
	 * @global	none
	 * @access	public
	 * @return	bool
	 * @update	2011/3/24
	*/
	public function set_cache_time($time)
	{
	    $this->_cache_time = (int)$time;
		return $this;
	} // end func


	/**
	 * 设置缓存路径
	 *
	 * @param	string $cache_path
	 * @access	public
	 * @return	bool
	 * @update	2011/3/24
	*/
	public function set_cache_path($cache_path)
	{
	    $this->_cache_path = substr($cache_path, - 1, 1) == '/' ? $cache_path : $cache_path . '/';
        return $this;
	} // end func



	/**
	 * 新建缓存
	 *
	 * @param	string $cache_name
	 * @param	json $data
	 * @access	public
	 * @return	void
	 * @update	2011/3/24
	*/
	public function save_cache($cache_name, $data)
	{
	    if ($this->_cache_time)
	    {
	        if ( ! is_dir($this->_cache_path))
	        {
	            mkdir($this->_cache_path,0777);
	        }
			$filename =$this->_cache_path . '/' . $cache_name . '.cache';
            @file_put_contents($filename, $data);
	    }
	} // end func


	/**
	 * 获取缓存
	 *
	 * @param	string $cache_name
	 * @access	public
	 * @return	string
	 * @update	2011/3/24
	*/
	public function get_cache($cache_name)
	{
		$filename =$this->_cache_path . '/' . $cache_name . '.cache';
	    if ($this->_cache_time)
	    {
	        if (file_exists($filename))
	        {
	            $filetime = date('U', filemtime($filename)); //获取文件创建时间
                $cachetime = $this->_cache_time * 60 * 60;
				if ($this->_cache_time != 0 && (time() - $filetime) > $cachetime)
				{
                    return false;
                }
                return @file_get_contents($filename);
	        }else {
	            return false;
	        }
	    }
	} // end func

	/**
	 * 清除缓存
	 *
	 * @param	string $cache_name
	 * @access	public
	 * @return	void
	 * @update	2011/3/24
	*/
	public function clear_cache($cache_name = Null)
	{
	    if ($cache_name)
	    {
	        @unlink($this->_cache_path . '/' . $cache_name . '.cache');
	    }
	} // end func

	/**
	 * 自动清除缓存
	 *
	 * @param	string $path
	 * @access	public
	 * @return	bool
	 * @update	2011/3/25
	*/
	public function auto_clear_cache($path = "")
	{
	    $path = $path ? $path : $this->_cache_path;

		if (empty($path))
		{
			return false;
		}

		if ($this->_cache_time)
		{
			if ( ! is_dir($path))
			{
				return false;
			}

			if ($fdir = opendir($path))
			{
				$old_cwd = getcwd();
				chdir($path);
				$path = getcwd().'/';
				while (($file = readdir($fdir)) !== false)
				{
					if ( in_array($file,array('.','..')))
					{
						continue;
					}
					if ( is_dir($path.$file))
					{
						$this->auto_clear_cache($path.'/'.$file.'/');
					}else {
						if (strpos($file,".cache") !== false)
						{
							$filetime = date('U', filemtime($path.$file));
							$cachetime = $this->_cache_time * 60 * 60;
							if ($this->_cache_time != 0 && (time() - $filetime) > $cachetime)
							{
								@unlink($path.$file);
							}
						}
					}
				}
				closedir($fdir);
				chdir($old_cwd);
			}
		}
	} // end func

} // end class
